#!/bin/bash

#---------------设置ssh免密登录多台设备---------------#
#设置要登入的节点ip地址
HOST_IP_NODE=(ip1 ip2 ip3 ... ipn)
HOST_PASS_NODE=(ps1 ps2 ps3 ... psn)

#安装expect软件包
yum install expect -y

#生成密钥
expect << EOF
set timeout 5
spawn ssh-keygen -t rsa
expect "id_rsa):" 
send "\r" 
expect "passphrase):" 
send "\r"
expect "again:"
send "\r" 
expect eof
EOF

#分发公钥 
copy-id(){
expect << EOF
set timeout 5
spawn ssh-copy-id root@$1
expect "(yes/no)?" 
send "yes\r"
expect "password:" 
send "$2\r" 
expect eof
EOF
}
 
#循环遍历
num=${#HOST_IP_NODE[*]}
fun(){
    for i in 0 1 ... $num; do
        ssh-copy-id root@${HOST_IP_NODE[i]} ${HOST_PASS_NODE[i]}
    done
}